#include "UI.h"
#include <graphics.h>
#include <conio.h>
#include <windows.h>

// 点击 "Click me" 按钮的回调函数
void onButtonClick() {
    std::cout << "Button clicked!" << std::endl;
}

// 点击 "Exit" 按钮的回调函数
void onExitButtonClick() {
    std::cout << "Exiting program..." << std::endl;
    PostQuitMessage(0);
}

int main() {
    initgraph(800, 600);

    UILayoutManager layoutManager;
    UIEventHandler eventHandler(layoutManager);

    // 创建一个按钮
    Button* button = new Button(100, 100, 200, 50, "Click me");
    button->setFontSize(24);  // 设置字体大小
    button->setTextColor(RED);  // 设置文本颜色
    button->setFillColor(YELLOW);  // 设置填充颜色
    button->setFontName("宋体");  // 设置字体名称
    button->setClickCallback(onButtonClick);  // 设置点击回调函数
    layoutManager.addElement(button);

    // 创建一个文本框
    TextBox* textBox = new TextBox(100, 200, 200, 50, "Hello, World!");
    textBox->setFontSize(18);
    textBox->setTextColor(GREEN);
    textBox->setFillColor(LIGHTBLUE);
    textBox->setFontName("宋体");
    layoutManager.addElement(textBox);

    // 创建一个退出按钮
    Button* exitButton = new Button(100, 300, 200, 50, "Exit");
    exitButton->setFontSize(24);
    exitButton->setTextColor(WHITE);
    exitButton->setFillColor(RED);
    exitButton->setFontName("宋体");
    exitButton->setClickCallback(onExitButtonClick);  // 设置点击回调函数
    layoutManager.addElement(exitButton);

    // 绘制所有 UI 元素
    layoutManager.drawAll();

    // 消息循环
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);

        switch (msg.message) {
        case WM_KEYDOWN:
            if (msg.wParam == VK_ESCAPE) {  // 按下 ESC 键退出程序
                PostQuitMessage(0);
            }
            break;
        case WM_LBUTTONDOWN:
            int mouseX = LOWORD(msg.lParam);
            int mouseY = HIWORD(msg.lParam);
            eventHandler.handleMouseClick(mouseX, mouseY);
            break;
        }
    }

    // 释放动态分配的内存
    delete button;
    delete textBox;
    delete exitButton;

    closegraph();
    return 0;
}